package com.xdtech.cc.init;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xdtech.common.service.impl.BaseService;
import com.xdtech.common.utils.EmptyUtil;
import com.xdtech.core.init.SysInitOperation;
import com.xdtech.core.model.BaseModel;
import com.xdtech.sys.model.MenuFunction;
import com.xdtech.sys.model.Operation;
import com.xdtech.sys.model.Params;

public class CcInit implements SysInitOperation {
	Log log = LogFactory.getLog(CcInit.class);
	BaseService<BaseModel> baseService;

	@Override
	public void initingToDb(BaseService<BaseModel> baseService) {
		this.baseService = baseService;
		List params = (List) baseService.findByHql("from Params where code=? and codeValue=?", "CcInit","true");
		log.info("信用卡管理模块数据库初始化......");
		if (EmptyUtil.isEmpty(params)) {
			initData();
			Params p = new Params("CcInit", "true", "信用卡管理数据库初始化标记");
			baseService.save(p);
			log.info("信用卡管理数据库初始化......结束");
		}else {
			log.info("信用卡管理数据库已经初始化......完毕");
		}
		
	}

	private void initData() {
		MenuFunction m1 = new MenuFunction();
		m1.setIconName("icon-save");
		m1.setNameCN("信用卡管理");
		m1.setOperationCode("card-manage");
		m1.setPageUrl(null);
		baseService.save(m1);
		
		MenuFunction m11 = new MenuFunction();
		m11.setIconName("icon-save");
		m11.setNameCN("客户账号");
		m11.setOperationCode("account-manage");
		m11.setPageUrl("account.do?account");
		m11.setParent(m1);
		baseService.save(m11);
		
		createButtonOperations("新增","cc:account:add",m11);
		createButtonOperations("删除","cc:account:delete",m11);
		createButtonOperations("编辑","cc:account:edit",m11);
		createButtonOperations("刷新","cc:account:refresh",m11);
		
		MenuFunction m12 = new MenuFunction();
		m12.setIconName("icon-save");
		m12.setNameCN("交易信息");
		m12.setOperationCode("account-trade-manage");
		m12.setPageUrl("tradeRecord.do?tradeRecord");
		m12.setParent(m1);
		baseService.save(m12);
		
		createButtonOperations("新增","cc:tradeRecord:add",m12);
		createButtonOperations("删除","cc:tradeRecord:delete",m12);
		createButtonOperations("编辑","cc:tradeRecord:edit",m12);
		createButtonOperations("刷新","cc:tradeRecord:refresh",m12);
		
		MenuFunction m13 = new MenuFunction();
		m13.setIconName("icon-save");
		m13.setNameCN("刷卡统计");
		m13.setOperationCode("pay-card-manage");
		m13.setPageUrl("payCard.do?payCard");
		m13.setParent(m1);
		baseService.save(m13);
	}
	
	/**
	 * 
	 * @author max.zheng
	 * @create 2015-2-15下午10:29:11
	 * @modified by
	 * @param m11
	 */
	private void createButtonOperations(String nameCN, String operationCode,
			MenuFunction menuFunction) {
		Operation operation = new Operation(nameCN, operationCode, menuFunction);
		baseService.save(operation);
	}

	@Override
	public void initingToCache(BaseService<BaseModel> baseService) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void initingToWebApplication(BaseService<BaseModel> baseService) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int getInitOrder() {
		// TODO Auto-generated method stub
		return 1000;
	}

}
